<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="icon" href="./img/husky_tran.png" type="image/x-icon"/>
    <link rel="stylesheet" type="text/css" href="./css/login.css">
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: "Helvetica Neue", Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "MicrosoftYaHei", "WenQuanYi Micro Hei", sans-serif;
        }
    </style>
</head>
<body>

<div class="login" style="margin-top: 15vh">
    <div class="login-triangle"></div>

    <!--    <img width="50px" src="./img/husky_tran.png" style="float: left;margin-left: 100px;margin-top: 10px" />-->
    <h2 class="login-header">注册</h2>

    <form class="login-container">
        <p><input type="text" placeholder="用户名" id="username"></p>
        <p><input type="email" placeholder="Email" id="email"></p>
        <p><input type="password" placeholder="密码" id="password"></p>
        <p><input type="password" placeholder="验证密码" id="password1" onkeyup="validate()"></p>
        <p>
        <div id="tishi" style="margin-left: 15px"></div>
        </p>
        <p><input type="button" value="注册" onclick="register()"></p>
    </form>
</div>
<script>
    // localStorage.clear();
    // window.localStorage.removeItem('reg_username')

    function register() {
        var host = 'http://124.70.36.92:19132/'
        // var host = 'http://localhost:8000/';
        var url = host + 'authorities/register/'

        var logindata = new FormData()
        var username = document.getElementById('username').value;
        var password = document.getElementById('password').value;
        var email = document.getElementById('email').value;
        logindata.append('username', username);
        logindata.append('password', password);
        logindata.append('email', email);
        console.log(logindata)

        axios({
            method: 'POST',
            url: url,
            // url: 'http://124.70.36.92:19132/polls/login/',
            dataType: 'json',
            data: logindata,
        }).then(res => {
            if (res.data.msg.length === 0) {
                console.log(res.data)
                console.log(res.data.data.username)
                // window.localStorage
                // localStorage.setItem('reg_username', JSON.stringify(res.data.data.username));
                localStorage.setItem('reg_username', res.data.data)
                // let locals = localStorage.getItem('userdata');
                // console.log('locals: ' + locals);

                window.location.href = "login.html";
            } else {
                console.log(res.data.msg)
                alert(res.data.msg)
            }
        }).catch(err => {
            console.log(err)
        })
    }

    function validate() {
        var pwd1 = document.getElementById("password").value;
        var pwd2 = document.getElementById("password1").value;
        <!-- 对比两次输入的密码 -->
        if (pwd1 === pwd2) {
            document.getElementById("tishi").innerHTML = "";
            // document.getElementById("button").disabled = false;
        } else {
            document.getElementById("tishi").innerHTML = "<font color='red'>两次密码不相同</font>";
            // document.getElementById("register").disabled = true;
        }

    }


</script>
</body>

</html>